package com.netflix.discovery;

public final class ServerStatusEvaluators {

    private static final ServerStatusEvaluator LEGACY_EVALUATOR = new ServerStatusEvaluator() {
        @Override
        public boolean accept(int statusCode, EurekaHttpClientDecorator.RequestType requestType) {
            if (statusCode >= 200 && statusCode < 300 || statusCode == 302) {
                return true;
            } else if (requestType == EurekaHttpClientDecorator.RequestType.Register && statusCode == 404) {
                return true;
            } else if (requestType == EurekaHttpClientDecorator.RequestType.SendHeartBeat && statusCode == 404) {
                return true;
            } else if (requestType == EurekaHttpClientDecorator.RequestType.Cancel) {  // cancel is best effort
                return true;
            } else if (requestType == EurekaHttpClientDecorator.RequestType.GetDelta && (statusCode == 403 || statusCode == 404)) {
                return true;
            }
            return false;
        }
    };

    private static final ServerStatusEvaluator HTTP_SUCCESS_EVALUATOR = new ServerStatusEvaluator() {
        @Override
        public boolean accept(int statusCode, EurekaHttpClientDecorator.RequestType requestType) {
            return statusCode >= 200 && statusCode < 300;
        }
    };

    private ServerStatusEvaluators() {
    }

    public static ServerStatusEvaluator legacyEvaluator() {
        return LEGACY_EVALUATOR;
    }

    public static ServerStatusEvaluator httpSuccessEvaluator() {
        return HTTP_SUCCESS_EVALUATOR;
    }
}
